// Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov  Date: 2010-5-13 14:13:04
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   RolesDaoImpl.java

package com.stnet.dao.enterprice.system;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Authorities;
import com.stnet.entity.Roles;
import com.stnet.entity.RolesAuthorities;

// Referenced classes of package org.stnet.dao.enterprice.system:
//            RolesDao

public class RolesDaoImpl extends BaseDaoImpl implements RolesDao {

	public RolesDaoImpl() {
	}

	public void delete(Roles roles) {
		getHibernateTemplate().delete(roles);
	}

	public void delete(Long id) {
		getHibernateTemplate().delete(get(id));
	}

	public List findAll() {
		List find = getHibernateTemplate().find("from Roles");
		getSessionFactory().getCurrentSession().flush();
		return find;
	}

	public List findAllBYState(String state) {
		List find = getHibernateTemplate().find("from Roles where state = ?",
				state);
		getSessionFactory().getCurrentSession().flush();
		return find;
	}

	public Roles get(Long id) {
		return (Roles) getHibernateTemplate().get(Roles.class, id);
	}

	public void save(Roles roles) {
		getSessionFactory().getCurrentSession().clear();
		getHibernateTemplate().save(roles);
	}

	public void update(Roles roles) {
		getSessionFactory().getCurrentSession().clear();
		getHibernateTemplate().saveOrUpdate(roles);
	}

	public boolean isNameUnique(String name) {
		List temp = getHibernateTemplate().find("from Roles where name = ?",
				name.trim());
		getSessionFactory().getCurrentSession().flush();
		return temp.size() <= 0;
	}

	public Pager findRolesByType(int pageSize, int pageNo, String state,
			String type) {
		Session session = null;
		Pager pager = null;
		try {
			session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Roles.class);
			if (!"-1".equals(state))
				criteria.add(Restrictions.eq("state", state));
			if (!"-1".equals(type))
				criteria.add(Restrictions.eq("type", type));
			int rowCount = ((Integer) criteria.setProjection(
					Projections.rowCount()).uniqueResult()).intValue();
			criteria.setProjection(null);
			int startIndex = pageSize * (pageNo - 1);
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			List result = criteria.list();
			pager = new Pager(pageSize, pageNo, rowCount, result);
		} catch (Exception ex) {

		} finally {
			session.close();
		}

		return pager;
	}
	
	public List findAllByType(String state,String type){
		List find = getHibernateTemplate().find("from Roles where state = ? and type = "+type,
				state);
		getSessionFactory().getCurrentSession().flush();
		return find;
	}
	
}